package com.mtw.bbs.like.controller;



import com.mtw.bbs.common.core.constant.BusinessObjectType;
import com.mtw.bbs.common.core.security.UserDetails;
import com.mtw.bbs.common.core.security.context.SecurityContextHolder;
import com.mtw.bbs.common.core.vo.Result;
import com.mtw.bbs.constant.LikeConstant;
import com.mtw.bbs.constant.LikeOperateEnum;
import com.mtw.bbs.like.pojo.dto.LikeRecordDto;
import com.mtw.bbs.like.service.LikeRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户踩接口层
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("user/dislike")
public class UserDislikeController {


    private final LikeRecordService likeRecordService;






    /**
     * 文章踩
     */
    @PostMapping("/article/{id}")
    public Result dislikeArticle(@PathVariable("id") String articleId) {

        UserDetails user = SecurityContextHolder.getContext().getAuthentication();
        LikeRecordDto dto = new LikeRecordDto();
        dto.setObjectId(articleId);
        dto.setUserMid(user.getId());
        dto.setObjectType(BusinessObjectType.ARTICLE);
        dto.setLikeStatus(LikeConstant.DISLIKE_VALUE);
        LikeOperateEnum likeOperateEnum = likeRecordService.addLike(dto);
        return likeOperateEnum.isFail() ? Result.fail("操作失败"):Result.success();
    }


    /**
     * 文章取消踩
     */
    @DeleteMapping("/article/{id}")
    public Result cancelArticleDislike(@PathVariable("id") String articleId) {

        UserDetails user = SecurityContextHolder.getContext().getAuthentication();
        LikeRecordDto dto = new LikeRecordDto();
        dto.setObjectId(articleId);
        dto.setUserMid(user.getId());
        dto.setObjectType(BusinessObjectType.ARTICLE);
        dto.setLikeStatus(LikeConstant.CANCEL_DISLIKE_VALUE);
        LikeOperateEnum likeOperateEnum = likeRecordService.cancelLike(dto);
        return likeOperateEnum.isFail() ? Result.fail("操作失败"):Result.success();
    }


    /**
     * 评论踩
     */
    @PostMapping("/reply/{id}")
    public Result dislikeReply(@PathVariable("id") String replyId) {

        UserDetails user = SecurityContextHolder.getContext().getAuthentication();
        LikeRecordDto dto = new LikeRecordDto();
        dto.setObjectId(replyId);
        dto.setUserMid(user.getId());
        dto.setObjectType(BusinessObjectType.REPLY);
        dto.setLikeStatus(LikeConstant.DISLIKE_VALUE);
        LikeOperateEnum likeOperateEnum = likeRecordService.addLike(dto);
        return likeOperateEnum.isFail() ? Result.fail("操作失败"):Result.success();
    }


    /**
     * 评论取消踩
     */
    @DeleteMapping("/reply/{id}")
    public Result cancelReplyDislike(@PathVariable("id") String replyId) {

        UserDetails user = SecurityContextHolder.getContext().getAuthentication();
        LikeRecordDto dto = new LikeRecordDto();
        dto.setObjectId(replyId);
        dto.setUserMid(user.getId());
        dto.setObjectType(BusinessObjectType.REPLY);
        dto.setLikeStatus(LikeConstant.CANCEL_DISLIKE_VALUE);
        LikeOperateEnum likeOperateEnum = likeRecordService.cancelLike(dto);
        return likeOperateEnum.isFail() ? Result.fail("操作失败"):Result.success();
    }


}
